package tbdp.tool;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.Arrays;
import java.util.List;

/**
 * Created by xbkaishui on 16/10/16.
 */
public class Main {

    private static Logger log = Logger.getLogger(Main.class);

    public static void main(String[] args) {

        if (args.length != 1) {
            log.info("error no input dir");
            System.exit(-1);
        }

        String inputDir = args[0];
        //自动判断 sh 和 sh 的文件

        File stockDir = new File(inputDir);
        String[] files = stockDir.list();
        if (files.length < 2) {
            log.info("文件列表不全跳过");
            System.exit(-1);
        }
        Cleaner cleaner = new Cleaner();
        try {
            log.info(Arrays.deepToString(files));
            List<String> fileList = Arrays.asList(files);
            String shDir = "";
            if (fileList.contains("show2003.dbf")) {
                shDir = new File(stockDir, "show2003.dbf").getAbsolutePath();
            }
            if (StringUtils.isEmpty(shDir)) {
                if (fileList.contains("sh")) {
                    shDir = new File(stockDir, "sh").getAbsolutePath();
                }
            }

            //兼容新格式 上海新的读取 mktdt00.txt
            if(StringUtils.isEmpty(shDir)){
                if (fileList.contains("mktdt00.txt")) {
                    shDir = new File(stockDir, "mktdt00.txt").getAbsolutePath();
                }
            }

            String szDir = "";
            if (fileList.contains("sjshq.dbf")) {
                szDir = new File(stockDir, "sjshq.dbf").getAbsolutePath();
            }
            if (StringUtils.isEmpty(shDir)) {
                if (fileList.contains("sz")) {
                    szDir = new File(stockDir, "sz").getAbsolutePath();
                }
            }

            if (StringUtils.isNotEmpty(shDir)) {
                log.info("run shdir " + shDir);
                cleaner.cleanSH(shDir, stockDir.getName());
            }
            if (StringUtils.isNotEmpty(szDir)) {
                log.info("run szDir " + szDir);
                cleaner.cleanSZ(szDir, stockDir.getName());
            }

            log.info("all done");
        } finally {
            cleaner.close();
            System.exit(0);
        }

    }
}
